﻿Public Class HFAgregarProducto
    Public valor As Integer = 0
    Public Sub Mostrar()
        If guardar.Enabled = False Then
            guardar.Enabled = True
            cancelar.Enabled = True
            agregar.Enabled = False
            modificar.Enabled = False
            eliminar.Enabled = False
            imprimir.Enabled = False

        Else
            cancelar.Enabled = False
            guardar.Enabled = False
            agregar.Enabled = True
            modificar.Enabled = True
            eliminar.Enabled = True
            imprimir.Enabled = True


        End If
    End Sub
    Private Sub ProductosBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProductosBindingNavigatorSaveItem.Click
        Try
            Me.Validate()
            Me.ProductosBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.DbFireAndIceDataSet)
        Catch ex As Exception
            AlertControl1.Show(Owner, "ERROR", "No se pudo guardar el registro")
        End Try

    End Sub

    Private Sub HFAgregarProducto_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ProductosBindingNavigator.Visible = False
        Try
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.unidadescategoria' table. You can move, or remove it, as needed.
            Me.UnidadescategoriaTableAdapter.Fill(Me.DbFireAndIceDataSet.unidadescategoria)
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.unidadescategoria' table. You can move, or remove it, as needed.
            Me.UnidadescategoriaTableAdapter.Fill(Me.DbFireAndIceDataSet.unidadescategoria)
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.productos' table. You can move, or remove it, as needed.
            Me.ProductosTableAdapter.Fill(Me.DbFireAndIceDataSet.productos)
        Catch ex As Exception
            AlertControl1.Show(Owner, "ERROR", "No se pudieron cargar los registros")
        End Try
        IdproductoTextBox.ReadOnly = True
        DescripcionTextBox.ReadOnly = True
        ComboBox1.Enabled = False

    End Sub

    Private Sub agregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles agregar.Click
        BindingNavigatorAddNewItem.PerformClick()
        Mostrar()
        DescripcionTextBox.ReadOnly = False
        ComboBox1.Enabled = True
        ExistenciaTextBox.Text = 0
    End Sub

    Private Sub modificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles modificar.Click

        Mostrar()
        DescripcionTextBox.ReadOnly = False
        ComboBox1.Enabled = True
        valor = 1
    End Sub

    Private Sub eliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles eliminar.Click
        Dim opcion As Integer

        opcion = MsgBox("¿Está seguro que desea eliminar el registro?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "Eliminar registro")
        If opcion = vbYes Then
            Try

                ProductosTableAdapter.DeleteQuery(Val(IdproductoTextBox.Text))
                AlertControl1.Show(Owner, "Productos", "Se eliminaron los datos correctamente")
                Me.ProductosTableAdapter.Fill(Me.DbFireAndIceDataSet.productos)
            Catch ex As Exception

                AlertControl1.Show(Owner, "ERROR", "Lo sentimos, el registro no puede ser eliminado ya que está relacionado con otras tablas")

            End Try
        End If

    End Sub

    Private Sub guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles guardar.Click
        Dim opcion As Integer




        If DescripcionTextBox.Text.Length = 0 Then
            ErrorProvider1.SetError(DescripcionTextBox, "Debe ingresar una descripcion del Producto")
        Else
            If ComboBox1.Text = "" Then
                ErrorProvider1.SetError(ComboBox1, "Debe de seleccionar una Tipo de Unidad")
            Else
                If valor <> 1 Then
                    opcion = MsgBox("¿Desea ingresar este producto a items?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "Eliminar registro")
                    If opcion = vbYes Then
                        Mostrar()
                        DetalleProductos.ShowDialog()

                    End If

                    If opcion = vbNo Then
                        ErrorProvider1.SetError(ComboBox1, Nothing)
                        ErrorProvider1.SetError(DescripcionTextBox, Nothing)
                        ProductosBindingNavigatorSaveItem.PerformClick()
                        Mostrar()
                        DescripcionTextBox.ReadOnly = True
                        ComboBox1.Enabled = False

                    End If
                Else
                    ErrorProvider1.SetError(ComboBox1, Nothing)
                    ErrorProvider1.SetError(DescripcionTextBox, Nothing)
                    ProductosBindingNavigatorSaveItem.PerformClick()
                    Mostrar()
                    DescripcionTextBox.ReadOnly = True
                    ComboBox1.Enabled = False
                End If
                
            End If

        End If
    End Sub

    Private Sub cancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancelar.Click
        Mostrar()
        DescripcionTextBox.ReadOnly = True
        ComboBox1.Enabled = False
    End Sub

    Private Sub ComboBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
        ComboBox1.AutoCompleteSource = AutoCompleteSource.ListItems
        ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
        ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
    End Sub

    Private Sub GroupControl1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles GroupControl1.Paint

    End Sub
End Class